iT邦幫忙

0

[Day 23]碼錶(Stopwatch)+圈速(Lap)GUI2

  • 分享至 

  • xImage
  •  

今天來補充碼表可以添加的功能(想加就加)
A. 一鍵把圈速複製到剪貼簿

# 插在 GUI 區塊附近,新增按鈕:
ttk.Button(btns, text="複製圈速", command=lambda: copy_laps()).grid(row=0, column=3, padx=4)

# 在檔案尾端附近加上:
def copy_laps():
    items = lap_list.get(0, tk.END)
    text = "\n".join(reversed(items))  # 由舊到新
    root.clipboard_clear()
    root.clipboard_append(text)

實作:
https://ithelp.ithome.com.tw/upload/images/20251008/20169368dqp4C94ih4.png

B. 匯出圈速為 CSV

# 匯出按鈕
from tkinter import filedialog
ttk.Button(btns, text="匯出 CSV", command=lambda: export_csv()).grid(row=0, column=4, padx=4)

def export_csv():
    import csv
    path = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV","*.csv")], initialfile="laps.csv")
    if not path: return
    rows = [("Lap","LapTime","Total")]
    # Listbox 顯示是「新到舊」,輸出時改成「舊到新」
    for i, line in enumerate(reversed(lap_list.get(0, tk.END)), 1):
        # 解析格式:Lap XX  單圈 mm:ss.cc   累計 mm:ss.cc
        parts = line.split()
        lap = i
        lap_time = parts[3]
        total = parts[5]
        rows.append((lap, lap_time, total))
    with open(path, "w", newline="", encoding="utf-8") as f:
        csv.writer(f).writerows(rows)

https://ithelp.ithome.com.tw/upload/images/20251008/201693688p7F1SLJzb.png
打包成單一執行檔(選用)
想給別人直接用(免裝 Python):

pip install pyinstaller
pyinstaller --noconsole --onefile stopwatch_gui.py
# 產物在 dist/stopwatch_gui

https://ithelp.ithome.com.tw/upload/images/20251008/20169368lzmPr28ADn.png

今日小結

  • 以 Tkinter 做出一個實用的碼錶+圈速工具
  • 使用 time.perf_counter() 確保計時準確
  • 用 after 做 UI 更新,暫停與重置時記得取消排程
  • 提供了剪貼簿與 CSV 的加值功能骨架,易於擴充

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言